<template>
    <van-icon name="shopping-cart"  size="34px" @click="addCart"/>
</template>


<script  setup>
import {computed, defineProps} from 'vue'
import { useStore } from 'vuex'
import { showSuccessToast,showFailToast } from 'vant'

let store = useStore()
const props = defineProps(['item'])

function addCart(){
    let {id,title,price,image} = props.item
    let cartList = computed(()=>{
        return store.getters['cartModule/getCartList']
    })
    // console.log(id,title,price,image);
    let cartItem = {
        id,
        title,
        price,
        image,
        count:1,
        check:false
    }
    let token = computed(()=>{
        return store.getters['loginModule/getUserInfo']
    })
    if(token.value.id){
        let cartFind = cartList.value.findIndex(item=> item.id === id)
        if(cartFind === -1){
            store.commit('cartModule/addCartList',cartItem)
            // console.log(cartList.value);
        }else{
            store.commit('cartModule/changeCount',id)
            // console.log(cartList.value);
        }
        showSuccessToast('添加购物车\n成功')
    }else{
        showFailToast('请先登录')
    }

    
}
</script>


<style lang="less" scoped>

</style>